import Router from "@koa/router";
import type { ControllerCallType } from "@/utils/controller";
import authenticate from "@/middlewares/authenticate";
import authorize from "@/middlewares/authorize";
import { PERMISSION_MAP } from "@/config";
import UserController from "@/controller/user";

export default (router: Router, controller: ControllerCallType) => {
  const r = new Router();
  const c = new UserController();

  r.prefix("/user");
  r.post(
    "/",
    authenticate,
    authorize([PERMISSION_MAP.UserWrite]),
    controller(c, "insert")
  );
  r.delete(
    "/:id",
    authenticate,
    authorize([PERMISSION_MAP.UserDelete]),
    controller(c, "remove")
  );
  r.patch(
    "/:id",
    authenticate,
    authorize([PERMISSION_MAP.UserWrite]),
    controller(c, "patch")
  );
  r.put(
    "/:id",
    authenticate,
    authorize([PERMISSION_MAP.UserWrite]),
    controller(c, "put")
  );
  r.get(
    "/:id",
    authenticate,
    authorize([PERMISSION_MAP.UserRead]),
    controller(c, "get")
  );
  r.get(
    "/list/page",
    authenticate,
    authorize([PERMISSION_MAP.UserRead]),
    controller(c, "page")
  );

  router.use(r.routes()).use(r.allowedMethods());
};
